class WidgetController extends ControllerBase { 
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ widgetInstanceList: Widget.list( params ) ]
    }

    def show = {
        def widgetInstance = Widget.get( params.id )

        if(!widgetInstance) {
            flash.message = "Widget not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ widgetInstance : widgetInstance ] }
    }

    def delete = {
        def widgetInstance = Widget.get( params.id )
        if(widgetInstance) {
            def person = widgetInstance.owner

	    Widget.withTransaction { status -> 
	       if (person.widgetCount && person.widgetCount > 0){
	          person.widgetCount -= 1
	       }
	       person.removeFromWidgets(widgetInstance)
               widgetInstance.delete()
	    }
	    if (session.widgetId == widgetInstance.id) {
	       session.widgetId = null;
	    }
            flash.message = "Widget ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Widget not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def widgetInstance = Widget.get( params.id )

        if(!widgetInstance) {
            flash.message = "Widget not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ widgetInstance : widgetInstance ]
        }
    }

    def update = {
        def widgetInstance = Widget.get( params.id )
	def owner = widgetInstance?.owner
        if(widgetInstance && owner && owner.id == session.personId) {
            widgetInstance.properties = params
            if(!widgetInstance.hasErrors() && widgetInstance.save()) {
                flash.message = "Widget ${params.id} updated"
                redirect(action:show,id:widgetInstance.id)
            }
            else {
                render(view:'edit',model:[widgetInstance:widgetInstance])
            }
        }
        else {
            flash.message = "Widget not updated"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def widgetInstance = new Widget()
        widgetInstance.properties = params
        return ['widgetInstance':widgetInstance]
    }

    def save = {
        def widgetInstance = new Widget(params)
	def person = sessionPerson()
	if (person) {
	   if(!widgetInstance.hasErrors() && widgetInstance.save()) {
                boolean result = false
		   Widget.withTransaction { status ->
		       if (person.widgetCount == null) {
		           person.widgetCount = 0
		       }
		       person.widgetCount++
		       person.save()
		       result = widgetInstance.save()
		   }
	       if (result) {
		  session.widgetId = widgetInstance.id
		  session.widgetCount = person.widgetCount
  	          flash.message = "Widget ${widgetInstance.id} created"
	          redirect(action:show,id:widgetInstance.id)
  	      } else {
	       render(view:'create',model:[widgetInstance:widgetInstance])
	   }
	} else {
            flash.message = "Widget not created"
            redirect(action: list)
       }
    }
  }

    def ctrliwg = {
        def widgetInstance = Widget.get( params.id )

        if(!widgetInstance) {
	    // XXX This is bogus, should be 404 instead, and certainly no flashing
            flash.message = "Widget not found with id ${params.id}"
            redirect(action:list)
        } else {
            def iwgString = widgetInstance.toControlIwgXml();
            System.out.println("iwgstring = ${iwgString}\n");
            render(text:iwgString, contentType:"application/ilabs-widger+xml");
        }
    }
}
